/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFAtualizacoes.java
 *
 * Created on 19/12/2012, 22:43:57
 */
package atualizacoes;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import oracle.toplink.essentials.exceptions.DatabaseException;

/**
 *
 * @author Douglas
 */
public class JFAtualizacoes extends JPanel {
    
    public JFAtualizacoes() {
       try{
        initComponents();
       }catch (DatabaseException dbex){
                JFConexao frame = new JFConexao();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                
                frame.pack();
                frame.setLocationRelativeTo(null);   
                frame.setVisible(true);             
       } 
        
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = javax.persistence.Persistence.createEntityManagerFactory(JFConexao.prefs.get("unidadePersistencia", null).toString().concat("PU"),JFConexao.prefsPU).createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT a FROM Atualizacoes a");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        revisoesQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT r FROM Revisoes r");
        revisoesList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(revisoesQuery.getResultList());
        tiposAtualizacoesQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT t FROM TiposAtualizacoes t");
        tiposAtualizacoesList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(tiposAtualizacoesQuery.getResultList());
        rotinasQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT r FROM Rotinas r");
        rotinasList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(rotinasQuery.getResultList());
        atualizacoes1 = new atualizacoes.Atualizacoes();
        historicos1 = new atualizacoes.Historicos();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        detailScrollPane = new javax.swing.JScrollPane();
        detailTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        titulojTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        urgenciajComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        revisaojComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        codTipoAtualizacaojComboBox = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jComboBox4 = new javax.swing.JComboBox();
        jToolBar1 = new javax.swing.JToolBar();
        newButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        sairjButton = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        deleteDetailButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        tituloBuscajTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        revisaojComboBoxBusca = new javax.swing.JComboBox();
        buscarPorRevisaojButton = new javax.swing.JButton();
        rotinajComboBoxBusca = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        buscarPorRotinajButton = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codAtualizacao}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("codAtualizacao"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codRevisao}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("codRevisao"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codRotina}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("codRotina"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codTipoAtualizacao}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("codTipoAtualizacao"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.datLanc}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("datLanc"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.titulo}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("titulo"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.urgencia}"), atualizacoes1, org.jdesktop.beansbinding.BeanProperty.create("urgencia"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codAtualizacao}"), historicos1, org.jdesktop.beansbinding.BeanProperty.create("codAtualizacao"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codHistorico}"), historicos1, org.jdesktop.beansbinding.BeanProperty.create("codHistorico"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codRotina}"), historicos1, org.jdesktop.beansbinding.BeanProperty.create("codRotina"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.datLanc}"), historicos1, org.jdesktop.beansbinding.BeanProperty.create("datLanc"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.historico}"), historicos1, org.jdesktop.beansbinding.BeanProperty.create("historico"));
        bindingGroup.addBinding(binding);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codAtualizacao}"));
        columnBinding.setColumnName("# ID");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${titulo}"));
        columnBinding.setColumnName("Titulo");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${urgencia}"));
        columnBinding.setColumnName("Urgencia");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);
        masterTable.getColumnModel().getColumn(0).setMinWidth(50);
        masterTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        masterTable.getColumnModel().getColumn(0).setMaxWidth(50);
        masterTable.getColumnModel().getColumn(2).setMinWidth(70);
        masterTable.getColumnModel().getColumn(2).setPreferredWidth(70);
        masterTable.getColumnModel().getColumn(2).setMaxWidth(70);

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedElement.historicosList}");
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, eLProperty, detailTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${codHistorico}"));
        columnBinding.setColumnName("# ID");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${datLanc}"));
        columnBinding.setColumnName("Lançado em");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${historico}"));
        columnBinding.setColumnName("Historico");
        columnBinding.setColumnClass(String.class);
        jTableBinding.setSourceUnreadableValue(java.util.Collections.emptyList());
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        detailScrollPane.setViewportView(detailTable);
        detailTable.getColumnModel().getColumn(0).setMinWidth(50);
        detailTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        detailTable.getColumnModel().getColumn(0).setMaxWidth(50);
        detailTable.getColumnModel().getColumn(1).setMinWidth(90);
        detailTable.getColumnModel().getColumn(1).setPreferredWidth(90);
        detailTable.getColumnModel().getColumn(1).setMaxWidth(90);

        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Título da atualização");
        jLabel1.setName("tituloLabel"); // NOI18N

        titulojTextField.setName("tituloField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, atualizacoes1, org.jdesktop.beansbinding.ELProperty.create("${titulo}"), titulojTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), titulojTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setText("Urgência");
        jLabel2.setName("urgenciaLabel"); // NOI18N

        urgenciajComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Crítica" }));
        urgenciajComboBox.setName("urgenciaField"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, atualizacoes1, org.jdesktop.beansbinding.ELProperty.create("${urgencia}"), urgenciajComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), urgenciajComboBox, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel3.setForeground(new java.awt.Color(0, 0, 255));
        jLabel3.setText("Revisão");
        jLabel3.setName("codRevisaoLabel"); // NOI18N

        revisaojComboBox.setName("codRevisaoField"); // NOI18N
        revisaojComboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Revisoes) {
                    Revisoes mec = (Revisoes)value;
                    setText(mec.getRevisao());
                }
                return this;
            }
        });

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, revisoesList, revisaojComboBox);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, atualizacoes1, org.jdesktop.beansbinding.ELProperty.create("${codRevisao}"), revisaojComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), revisaojComboBox, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel4.setForeground(new java.awt.Color(0, 0, 255));
        jLabel4.setText("Tipo");
        jLabel4.setToolTipText("");
        jLabel4.setName("codTipoAtualizacaoLabel"); // NOI18N

        codTipoAtualizacaojComboBox.setName("codTipoAtualizacaoField"); // NOI18N
        codTipoAtualizacaojComboBox.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof TiposAtualizacoes) {
                    TiposAtualizacoes mec = (TiposAtualizacoes)value;
                    setText(mec.getTipoAtualizacao());
                }
                return this;
            }
        });

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tiposAtualizacoesList, codTipoAtualizacaojComboBox);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, atualizacoes1, org.jdesktop.beansbinding.ELProperty.create("${codTipoAtualizacao}"), codTipoAtualizacaojComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), codTipoAtualizacaojComboBox, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jLabel5.setForeground(new java.awt.Color(0, 0, 255));
        jLabel5.setText("Rotina");
        jLabel5.setName("codRotinaLabel"); // NOI18N

        jComboBox4.setName("codRotinaField"); // NOI18N
        jComboBox4.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Rotinas) {
                    Rotinas mec = (Rotinas)value;
                    setText(mec.getRotina());
                }
                return this;
            }
        });

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, rotinasList, jComboBox4);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, atualizacoes1, org.jdesktop.beansbinding.ELProperty.create("${codRotina}"), jComboBox4, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), jComboBox4, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jToolBar1.setRollover(true);

        newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Novo.png"))); // NOI18N
        newButton.setText("Novo");
        newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        newButton.addActionListener(formListener);
        jToolBar1.add(newButton);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Salvar.png"))); // NOI18N
        saveButton.setText("Salvar");
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(formListener);
        jToolBar1.add(saveButton);

        refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Atualizar.png"))); // NOI18N
        refreshButton.setText("Atualizar");
        refreshButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        refreshButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        refreshButton.addActionListener(formListener);
        jToolBar1.add(refreshButton);

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Excluir.png"))); // NOI18N
        deleteButton.setText("Excluir");
        deleteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);
        jToolBar1.add(deleteButton);

        sairjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Sair2.png"))); // NOI18N
        sairjButton.setText("Sair");
        sairjButton.setFocusable(false);
        sairjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        sairjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        sairjButton.addActionListener(formListener);
        jToolBar1.add(sairjButton);

        jToolBar2.setRollover(true);

        deleteDetailButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Excluir.png"))); // NOI18N
        deleteDetailButton.setText("Remover");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteDetailButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteDetailButton.addActionListener(formListener);
        jToolBar2.add(deleteDetailButton);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Novo.png"))); // NOI18N
        jButton1.setText("Novo Histórico...");
        jButton1.setFocusable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.codAtualizacao != null}"), jButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jButton1.addActionListener(formListener);
        jToolBar2.add(jButton1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Check.png"))); // NOI18N
        jButton2.setText("Ver histórico...");
        jButton2.setFocusable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, detailTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.historico != null}"), jButton2, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jButton2.addActionListener(formListener);
        jToolBar2.add(jButton2);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Novo2.png"))); // NOI18N
        jButton3.addActionListener(formListener);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Novo2.png"))); // NOI18N
        jButton4.addActionListener(formListener);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Novo2.png"))); // NOI18N
        jButton5.addActionListener(formListener);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar"));

        jLabel6.setText("Titulo:");

        tituloBuscajTextField.addActionListener(formListener);
        tituloBuscajTextField.addKeyListener(formListener);

        jLabel7.setText("Revisão:");

        revisaojComboBoxBusca.setName("codRevisaoField"); // NOI18N
        revisaojComboBoxBusca.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Revisoes) {
                    Revisoes mec = (Revisoes)value;
                    setText(mec.getRevisao());
                }
                return this;
            }
        });

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, revisoesList, revisaojComboBoxBusca);
        bindingGroup.addBinding(jComboBoxBinding);

        revisaojComboBoxBusca.addItemListener(formListener);

        buscarPorRevisaojButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Buscar2.png"))); // NOI18N
        buscarPorRevisaojButton.addActionListener(formListener);

        rotinajComboBoxBusca.setName("codRevisaoField"); // NOI18N
        rotinajComboBoxBusca.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Rotinas) {
                    Rotinas mec = (Rotinas)value;
                    setText(mec.getRotina());
                }
                return this;
            }
        });

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, rotinasList, rotinajComboBoxBusca);
        bindingGroup.addBinding(jComboBoxBinding);

        rotinajComboBoxBusca.addItemListener(formListener);

        jLabel8.setText("Rotina:");

        buscarPorRotinajButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/atualizacoes/resources/Buscar2.png"))); // NOI18N
        buscarPorRotinajButton.addActionListener(formListener);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(tituloBuscajTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(161, 161, 161))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(revisaojComboBoxBusca, 0, 164, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarPorRevisaojButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(rotinajComboBoxBusca, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buscarPorRotinajButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buscarPorRevisaojButton)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(revisaojComboBoxBusca, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rotinajComboBoxBusca, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(buscarPorRotinajButton)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tituloBuscajTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buscarPorRevisaojButton, revisaojComboBoxBusca, tituloBuscajTextField});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(masterScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .addComponent(detailScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(titulojTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(urgenciajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(revisaojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(codTipoAtualizacaojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox4, 0, 267, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jToolBar2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(1, 1, 1)
                        .addComponent(titulojTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(1, 1, 1)
                        .addComponent(urgenciajComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(revisaojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3)
                    .addComponent(codTipoAtualizacaojComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(detailScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {codTipoAtualizacaojComboBox, jButton3, jButton4, jButton5, jComboBox4, revisaojComboBox});

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.ItemListener, java.awt.event.KeyListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == newButton) {
                JFAtualizacoes.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton) {
                JFAtualizacoes.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                JFAtualizacoes.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton) {
                JFAtualizacoes.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == sairjButton) {
                JFAtualizacoes.this.sairjButtonActionPerformed(evt);
            }
            else if (evt.getSource() == deleteDetailButton) {
                JFAtualizacoes.this.deleteDetailButtonActionPerformed(evt);
            }
            else if (evt.getSource() == jButton1) {
                JFAtualizacoes.this.jButton1ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton2) {
                JFAtualizacoes.this.jButton2ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton3) {
                JFAtualizacoes.this.jButton3ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton4) {
                JFAtualizacoes.this.jButton4ActionPerformed(evt);
            }
            else if (evt.getSource() == jButton5) {
                JFAtualizacoes.this.jButton5ActionPerformed(evt);
            }
            else if (evt.getSource() == tituloBuscajTextField) {
                JFAtualizacoes.this.tituloBuscajTextFieldActionPerformed(evt);
            }
            else if (evt.getSource() == buscarPorRevisaojButton) {
                JFAtualizacoes.this.buscarPorRevisaojButtonActionPerformed(evt);
            }
            else if (evt.getSource() == buscarPorRotinajButton) {
                JFAtualizacoes.this.buscarPorRotinajButtonActionPerformed(evt);
            }
        }

        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            if (evt.getSource() == revisaojComboBoxBusca) {
                JFAtualizacoes.this.revisaojComboBoxBuscaItemStateChanged(evt);
            }
            else if (evt.getSource() == rotinajComboBoxBusca) {
                JFAtualizacoes.this.rotinajComboBoxBuscaItemStateChanged(evt);
            }
        }

        public void keyPressed(java.awt.event.KeyEvent evt) {
            if (evt.getSource() == tituloBuscajTextField) {
                JFAtualizacoes.this.tituloBuscajTextFieldKeyPressed(evt);
            }
        }

        public void keyReleased(java.awt.event.KeyEvent evt) {
            if (evt.getSource() == tituloBuscajTextField) {
                JFAtualizacoes.this.tituloBuscajTextFieldKeyReleased(evt);
            }
        }

        public void keyTyped(java.awt.event.KeyEvent evt) {
            if (evt.getSource() == tituloBuscajTextField) {
                JFAtualizacoes.this.tituloBuscajTextFieldKeyTyped(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    public static Atualizacoes atualizacoes;
    
    private void deleteDetailButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteDetailButtonActionPerformed
        int index = masterTable.getSelectedRow();
        atualizacoes.Atualizacoes a = list.get(masterTable.convertRowIndexToModel(index));
        Collection<atualizacoes.Historicos> hs = a.getHistoricosList();
        int[] selected = detailTable.getSelectedRows();
        List<atualizacoes.Historicos> toRemove = new ArrayList<atualizacoes.Historicos>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            selected[idx] = detailTable.convertRowIndexToModel(selected[idx]);
            int count = 0;
            Iterator<atualizacoes.Historicos> iter = hs.iterator();
            while (count++ < selected[idx]) {
                iter.next();
            }
            atualizacoes.Historicos h = iter.next();
            toRemove.add(h);
            entityManager.remove(h);
        }
        hs.removeAll(toRemove);
        masterTable.clearSelection();
        masterTable.setRowSelectionInterval(index, index);
    }//GEN-LAST:event_deleteDetailButtonActionPerformed
        

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed

        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        tituloBuscajTextField.setText("");
        tituloBuscajTextFieldKeyReleased(null);        
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
    }//GEN-LAST:event_refreshButtonActionPerformed
    
    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<atualizacoes.Atualizacoes> toRemove = new ArrayList<atualizacoes.Atualizacoes>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            atualizacoes.Atualizacoes a = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(a);
            entityManager.remove(a);
        }
        list.removeAll(toRemove);
    }//GEN-LAST:event_deleteButtonActionPerformed
    
    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        atualizacoes.Atualizacoes a = new atualizacoes.Atualizacoes();
        entityManager.persist(a);
        list.add(a);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
        titulojTextField.grabFocus();
    }//GEN-LAST:event_newButtonActionPerformed
    
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        try {
            if (!Funcoes.isFormValido(this)) return;
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            
            //Atualizar na grade o id persistido
            int linha = masterTable.getSelectedRow();
            Funcoes.getAtualizarQuery(entityManager, query, list);
            masterTable.getSelectionModel().setSelectionInterval(linha, linha); 
            
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<atualizacoes.Atualizacoes> merged = new ArrayList<atualizacoes.Atualizacoes>(list.size());
            for (atualizacoes.Atualizacoes a : list) {
                merged.add(entityManager.merge(a));
            }
            list.clear();
            list.addAll(merged);
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
                atualizacoes = atualizacoes1;
                JDialog frame = new JDialog();
                frame.setContentPane(new JFHistoricos());
                frame.setTitle("Históricos");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setModal(true);
                frame.setVisible(true);

                int linha = masterTable.getSelectedRow();
                Funcoes.getAtualizarQuery(entityManager, query, list);
                masterTable.getSelectionModel().setSelectionInterval(linha, linha);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
        //Obter linha da grade detalhes
        int linha = detailTable.getSelectedRow();
        //Não está selecioanda ?
        if (linha == -1){
            //Seleciona a primeira.
            detailTable.getSelectionModel().setSelectionInterval(0, 0);
        }
        
        // A atribuição da variavel detalhes deve ser antes de criar o form
        JFDetalhesHistorico.detalhes = historicos1.getHistorico();
        JFDetalhesHistorico oFormDetalhes = new JFDetalhesHistorico();
        oFormDetalhes.pack();
       
        oFormDetalhes.setLocationRelativeTo(null);
        oFormDetalhes.setVisible(true);
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void sairjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairjButtonActionPerformed
        // TODO add your handling code here:
       Component comp = SwingUtilities.getRoot(this);
       ((Window) comp).dispose();
    }//GEN-LAST:event_sairjButtonActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if (!Funcoes.isFormValido(this)) return;  
        //Chamar JFrame em ShowModal
        JDialog frame = new JDialog();
        frame.setContentPane(new JFRotinas());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setModal(true);
        frame.setTitle("Rotinas");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Funcoes.getAtualizarQuery(entityManager, rotinasQuery, rotinasList);
           
                


    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if (!Funcoes.isFormValido(this)) return;   
        //Chamar JFrame em ShowModal
        JDialog frame = new JDialog();
        frame.setContentPane(new JFRevisoes());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setTitle("Revisões");
        frame.setModal(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Funcoes.getAtualizarQuery(entityManager, revisoesQuery, revisoesList);        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (!Funcoes.isFormValido(this)) return;
        //Chamar JFrame em ShowModal
        JDialog frame = new JDialog();
        frame.setContentPane(new JFTiposAtualizacoes());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setModal(true);
        frame.setTitle("Tipos de Atualizações");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Funcoes.getAtualizarQuery(entityManager, tiposAtualizacoesQuery, tiposAtualizacoesList); 
    }//GEN-LAST:event_jButton4ActionPerformed

    private void tituloBuscajTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tituloBuscajTextFieldKeyReleased
        //Montar query com consulta pre-definida na classe Atualizacoes.
        query = entityManager.createNamedQuery("Atualizacoes.findByTitulo");
        //Criar objeto da classe Atualizacoes
        Atualizacoes oAtualizacao = new Atualizacoes();
        //Definir o campo título do objeto instancia da classe o texto digitado concatenando um % para o like
        oAtualizacao.setTitulo(tituloBuscajTextField.getText().concat("%"));
        //Enviar parametro exigido pela consulta pre-definida.
        query.setParameter("titulo", oAtualizacao.getTitulo());
        //Funcao que atualiza a lista
        Funcoes.getAtualizarQuery(entityManager, query, list);

        //Se tecla enter pressionada
        if (evt != null){
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                //Obter quantidade de linha
                int quantLinha = masterTable.getRowCount();
                //Tem resultado ?
                if (quantLinha > 0) {
                    //Selecionar primeira linha
                    masterTable.getSelectionModel().setSelectionInterval(0, 0);
                }

            }                
        }
 

    }//GEN-LAST:event_tituloBuscajTextFieldKeyReleased

    private void tituloBuscajTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tituloBuscajTextFieldKeyPressed
 
    }//GEN-LAST:event_tituloBuscajTextFieldKeyPressed

    private void tituloBuscajTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tituloBuscajTextFieldKeyTyped

    }//GEN-LAST:event_tituloBuscajTextFieldKeyTyped

    private void tituloBuscajTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tituloBuscajTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tituloBuscajTextFieldActionPerformed

    private void revisaojComboBoxBuscaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_revisaojComboBoxBuscaItemStateChanged

 
    }//GEN-LAST:event_revisaojComboBoxBuscaItemStateChanged

    private void buscarPorRevisaojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarPorRevisaojButtonActionPerformed
        //Montar query com consulta pre-definida na classe Atualizacoes.
        query = entityManager.createNamedQuery("Atualizacoes.findByCodRevisao");
        //Criar objeto da classe Atualizacoes
        Atualizacoes oAtualizacao = new Atualizacoes();
        //Definir o campo codRevisao do objeto instancia da classe
        oAtualizacao.setCodRevisao((Revisoes) revisaojComboBoxBusca.getSelectedItem());
        //Enviar parametro exigido pela consulta pre-definida.
        query.setParameter("codRevisao", oAtualizacao.getCodRevisao());
        //Funcao que atualiza a lista
        Funcoes.getAtualizarQuery(entityManager, query, list);

        //Obter quantidade de linha
        int quantLinha = masterTable.getRowCount();
        //Tem resultado ?
        if (quantLinha > 0) {
            //Selecionar primeira linha
            masterTable.getSelectionModel().setSelectionInterval(0, 0);
        }
            
    }//GEN-LAST:event_buscarPorRevisaojButtonActionPerformed

    private void rotinajComboBoxBuscaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rotinajComboBoxBuscaItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_rotinajComboBoxBuscaItemStateChanged

    private void buscarPorRotinajButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarPorRotinajButtonActionPerformed
        //Montar query com consulta pre-definida na classe Atualizacoes.
        query = entityManager.createNamedQuery("Atualizacoes.findByCodRotina");
        //Criar objeto da classe Atualizacoes
        Atualizacoes oAtualizacao = new Atualizacoes();
        //Definir o campo codRevisao do objeto instancia da classe
        oAtualizacao.setCodRotina((Rotinas) rotinajComboBoxBusca.getSelectedItem());
        //Enviar parametro exigido pela consulta pre-definida.
        query.setParameter("codRotina", oAtualizacao.getCodRotina());
        //Funcao que atualiza a lista
        Funcoes.getAtualizarQuery(entityManager, query, list);

        //Obter quantidade de linha
        int quantLinha = masterTable.getRowCount();
        //Tem resultado ?
        if (quantLinha > 0) {
            //Selecionar primeira linha
            masterTable.getSelectionModel().setSelectionInterval(0, 0);
        }
    }//GEN-LAST:event_buscarPorRotinajButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private atualizacoes.Atualizacoes atualizacoes1;
    private javax.swing.JButton buscarPorRevisaojButton;
    private javax.swing.JButton buscarPorRotinajButton;
    private javax.swing.JComboBox codTipoAtualizacaojComboBox;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton deleteDetailButton;
    private javax.swing.JScrollPane detailScrollPane;
    private javax.swing.JTable detailTable;
    private javax.persistence.EntityManager entityManager;
    private atualizacoes.Historicos historicos1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private java.util.List<atualizacoes.Atualizacoes> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JComboBox revisaojComboBox;
    private javax.swing.JComboBox revisaojComboBoxBusca;
    private java.util.List<atualizacoes.Revisoes> revisoesList;
    private javax.persistence.Query revisoesQuery;
    private javax.swing.JComboBox rotinajComboBoxBusca;
    private java.util.List<atualizacoes.Rotinas> rotinasList;
    private javax.persistence.Query rotinasQuery;
    private javax.swing.JButton sairjButton;
    private javax.swing.JButton saveButton;
    private java.util.List<atualizacoes.TiposAtualizacoes> tiposAtualizacoesList;
    private javax.persistence.Query tiposAtualizacoesQuery;
    private javax.swing.JTextField tituloBuscajTextField;
    private javax.swing.JTextField titulojTextField;
    private javax.swing.JComboBox urgenciajComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFAtualizacoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFAtualizacoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFAtualizacoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFAtualizacoes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new JFAtualizacoes());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                
            }
        });
    }
}
